﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text;
using ExtAspNet;

namespace HY.WEB.SYS
{
    public partial class _menuset : PageBase
    {
        public string whe = "", stype = "";
        public string us;
        public string tab = "menu_safe";
        protected void Page_Load(object sender, EventArgs e)
        {
            us = Session["ID"].ToString();
            if (!IsPostBack)
            {
               // btnSearch.OnClientClick = Window1.GetShowReference("../function/search.aspx?tab="+tab+"", "高级搜索");
                fill();
                BindGrid();
            }
        }

        private void fill()
        {
            //DataTable d = BLL.BLLfun.get_search(tab);
            //for (int i = 0; i < d.Rows.Count; i++)
            //{
            //    ddlfiled.Items.Add(new ExtAspNet.ListItem(d.Rows[i][1].ToString(), d.Rows[i][0].ToString()));
            //}
            //ddlfiled.DataBind();

            //DataTable e = BLL.BLLfun.get_where();
            //for (int i = 0; i < e.Rows.Count; i++)
            //{
            //    ddlwhere.Items.Add(new ExtAspNet.ListItem(e.Rows[i][0].ToString(), e.Rows[i][1].ToString()));
            //}
            //ddlwhere.DataBind();


        }

        //删除
        protected void btnDelete_Click(object sender, EventArgs e)
        {


            int val = Grid1.PageIndex * Grid1.PageSize;
            StringBuilder sb = new StringBuilder();
            foreach (int row in Grid1.SelectedRowIndexArray)
            {
                sb.Append(Grid1.DataKeys[row + val][0].ToString());
                sb.Append(",");

            }

            Alert.ShowInTop("你选择了删除项：" + sb.ToString().TrimEnd(','));
            HY.BLL.Bllbase.DeleteList(tab, sb.ToString().TrimEnd(','));
            BindGrid();


        }

        protected void btnsavemenu_Click(object sender, EventArgs e)
        {

            if (txt1.Text == ""||hidsid.Text=="")
            {
                Alert.Show("请选择角色.");
                return;
            }

          //  Getval_void();
            Boolean tmp;
            ExtAspNet.CheckBoxField field2 = (ExtAspNet.CheckBoxField)Grid2.FindColumn("CBHYN");
            StringBuilder sb = new StringBuilder();
            for (int row =0; row <Grid2.Rows.Count;row++ )
            {
                tmp = field2.GetCheckedState(row);
                if (tmp)
                {
                    sb.Append(Grid2.DataKeys[row][0].ToString());
                    sb.Append(",");
                }

            }

            Alert.ShowInTop("你选择了菜单项：" + sb.ToString().TrimEnd(','));
            txt2.Text = sb.ToString().TrimEnd(',');
            this.btsave_Click(null,null);

            //StringBuilder sb = new StringBuilder();
            //foreach (int row in Grid2.SelectedRowIndexArray)
            //{
            //    sb.Append(Grid2.DataKeys[row][0].ToString());
            //    sb.Append(",");

            //}

            //Alert.ShowInTop("你选择了菜单项：" + sb.ToString().TrimEnd(','));
            //txt2.Text = sb.ToString().TrimEnd(',');
        


        }

        private void BindGrid()
        {
            DataTable  table;

            string sqltxt = "SELECT * FROM "+tab+"";

            table = DAL.DbHelperSQL.SqlDataAdapter(sqltxt);
            Grid1.DataSource = table;
            Grid1.DataBind();

            sqltxt = "SELECT  [sid] ,[owenid],[lev],[m_name] ,[m_link],[showstatus] ,[isstatus] ,"+
               "[isorder] ,[remark] ,[TreeLevel],1 as atvalid FROM sys_menu  order by remark,TreeLevel ";
            table = DAL.DbHelperSQL.SqlDataAdapter(sqltxt);
            Grid2.DataSource = table;
            Grid2.DataBind();
        }

      

        protected void Grid1_PageIndexChange(object sender, ExtAspNet.GridPageEventArgs e)
        {
            Grid1.PageIndex = e.NewPageIndex;
        }

        protected void Grid2_PageIndexChange(object sender, ExtAspNet.GridPageEventArgs e)
        {
            Grid2.PageIndex = e.NewPageIndex;
        }


        protected void Grid1_Sort(object sender, ExtAspNet.GridSortEventArgs e)
        {

            DataTable table = BLL.oa.bookmarks.get_list(us);
            //DataTable table = BLL.oa.ad.get_list(stype);

            DataView view1 = table.DefaultView;
            view1.Sort = String.Format("{0} {1}", e.SortField, e.SortDirection);

            Grid1.DataSource = view1;
            Grid1.DataBind();
        }


        protected void Grid2_Sort(object sender, ExtAspNet.GridSortEventArgs e)
        {

            DataTable table = BLL.Bllbase.get_table("sys_menu");
            //DataTable table = BLL.oa.ad.get_list(stype);

            DataView view1 = table.DefaultView;
            view1.Sort = String.Format("{0} {1}", e.SortField, e.SortDirection);

            Grid2.DataSource = view1;
            Grid2.DataBind();
        }

     
     


        protected void Grid1_RowCommand(object sender, ExtAspNet.GridCommandEventArgs e)
        {
             string DD = Grid1.DataKeys[e.RowIndex][0].ToString();

           // if (e.CommandName == "edit") //修改 
           // {
               
                DataTable dt1 = BLL.Bllbase.get_table(tab, "sid=" + DD + "");           
                hidsid.Text = dt1.Rows[0]["sid"].ToString().Trim();
                txt1.Text = dt1.Rows[0]["bname"].ToString().Trim();
               txt2.Text = dt1.Rows[0]["bmenu"].ToString().Trim();
             //  ddltype.SelectedIndex = ddltype.Items.IndexOf(ddltype.Items.FindByValue(dt1.Rows[0]["salsetype"].ToString()));
       
           //  }
           
            if (e.CommandName == "sub")//填充条件 
            {
              DataTable dt=BLL.Bllbase.Get_Menu_safe(DD);           
              Grid2.DataSource = dt;
              Grid2.DataBind();
            } 

        }

  
        protected void btsave_Click(object sender, EventArgs e)
        {
            string user=us;
            string t1 = txt1.Text.Trim();
            string t2 = txt2.Text.Trim();

            if (t1 == "" ) return;

         
            string hid = hidsid.Text;
            string sqltxt="";
            if (hid == "")
                sqltxt = "insert into " + tab + "(bname) values('" + t1 + "')";
            else
                sqltxt = "update " + tab + "  set bname='" + t1 + "',bmenu='"+t2+"' where sid="+hid;

            DAL.DbHelperSQL.ExecuteNonQuery(sqltxt);
            hidsid.Text = "";

            txt1.Text="";
            txt2.Text = "";

            BindGrid();
        }


        protected void Grid2_RowDataBound(object sender, GridRowEventArgs e)
        {
            //
           // ExtAspNet.CheckBox cb1 = Grid1.FindColumn("WindowField1") as ExtAspNet.WindowField;


        }





    }
}
